package com.service.calculate;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.LinkedList;

import com.service.bean.Road_GpsSignal;
import com.service.bean.Road_Vehicle;
import com.service.bean.signal.GpsSignalWork;
import com.service.other.ServerConfig;

/**
 * 计算运输车变化量
 */
public class DumperSignalWorkCal {
	/**
	 * 当前Gps数据
	 */
	private Road_GpsSignal signal;
	/**
	 * 车辆
	 */
	private Road_Vehicle vehicle;
	
	public DumperSignalWorkCal(Road_GpsSignal signal, Road_Vehicle vehicle) {
		this.signal = signal;
		this.vehicle = vehicle;
	}
	
	public GpsSignalWork cal() {
		GpsSignalWork work = null;

		if (null == vehicle.lastGps) {
			return null;
		}
			
		synchronized (vehicle.lastGps) {
				
			work = new GpsSignalWork();
			work.signalId = signal.signalId;
			work.gpsTime = signal.gpsTime;
			work.equipmentId = signal.equipmentId;
			work.isInWork = signal.isInWork;
			work.accStatus = signal.accStatus;
			work.speed = signal.speed;
			//
			getRealOilCal(work);
//			work.realOilCal = signal.oilAmtCal;
			work.areaId = signal.areaId;
			work.isInArea = (signal.areaId == null ? 0 : 1);
			work.totalMile = signal.totalMile;
			
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			// 当前Gps时间
			String current = sdf.format(signal.gpsTime);
			// 上一条Gps时间
			String last = sdf.format(vehicle.lastGps.gpsTime);
			if (current.equals(last)) {
				// 是同一天，开始计算变化量
				// 四舍五入保留两位小数
				double currentTotalMile = new BigDecimal(signal.totalMile).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				//double currentOilAmtCal = new BigDecimal(signal.oilAmtCal).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				double lastTotalMile = new BigDecimal(vehicle.lastGps.totalMile).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				//double lastOilAmtCal = new BigDecimal(vehicle.lastGps.oilAmtCal).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
				
				// 距离差，Gps计算的总里程
				double distanceDiff = currentTotalMile - lastTotalMile;
				// 时间差，单位秒
				long timeDiff = (signal.gpsTime.getTime() - vehicle.lastGps.gpsTime.getTime()) / 1000;
				//如果两条数据时间差大于设定时间,不做work统计
				if (timeDiff > ServerConfig.instance().intervaltime) {
					return null;
				}
				// 油量差
//				double oilAmtDiff = currentOilAmtCal - lastOilAmtCal;
				double oilAmtDiff=0;
				if( 0!=vehicle.lastGps.realOilCal ){
					oilAmtDiff = signal.realOilCal - vehicle.lastGps.realOilCal;
				}
				
				if(Math.abs(oilAmtDiff) >= ServerConfig.instance().oliEx){
					//油位变化达到突变值
					work.isOilEx = 1;
				}

				if (1 == signal.isInWork) {
					if (1 == vehicle.lastGps.isInWork) {
						work.workMile = distanceDiff;
						work.workTime = timeDiff;
						work.workOilChange = oilAmtDiff;
						if (0 == signal.speed && 0 == vehicle.lastGps.speed) {
							work.workStopTime = timeDiff;
						}
					}
				} else {
//					if (0 == vehicle.lastGps.isInWork) {
						work.noWorkMile = distanceDiff;
						work.noWorkTime = timeDiff;
						work.noWorkOilChange = oilAmtDiff;
						if (0 == signal.speed && 0 == vehicle.lastGps.speed) {
							work.noWorkStopTime = timeDiff;
						}
//					}
				}
				
				if (0 != signal.speed && 1 == signal.accStatus) {
					if (0 != vehicle.lastGps.speed && 1 == vehicle.lastGps.accStatus) {
						work.driveTime = timeDiff;
					}
				} else {
					if (0 == vehicle.lastGps.speed || 0 == vehicle.lastGps.accStatus) {
						work.stopTime = timeDiff;
					}
				}
				
				if (null != signal.areaId) {
					if (1 == signal.isInWork && 1 == vehicle.lastGps.isInWork) {
						if (signal.areaId.equals(vehicle.lastGps.areaId)) {
							work.areaKeepTime = timeDiff;
							work.areaKeepMile = distanceDiff;
						} else {
							work.areaKeepTime = 0;
							work.areaKeepMile = 0;
						}
						if (null != vehicle.lastGps.areaId) {
							work.allAreaKeepTime = timeDiff;
							work.allAreaKeepMile = distanceDiff;
						} else {
							work.outAreaKeepTime = timeDiff;
							work.outAllAreaKeepMile = distanceDiff;
						}
					}
				}
				if (1 == signal.accStatus && 1 == vehicle.lastGps.accStatus) {
					work.outWorkTime = timeDiff;
					work.outWorkMile = distanceDiff;
				}
				
				//油位变化量
				work.oilChange = oilAmtDiff;
				//里程变化量
				work.mileChange = distanceDiff;
				
			} else {
				work.oilChange = 0;
				work.mileChange = 0;
			}
			
			//停车时长的累计
			work.sumStopTime = work.stopTime == 0 ? 0 : vehicle.lastGps.sumStopTime + work.stopTime;
			//工作状态中，速度为0的时长的累计
			work.sumWorkStopTime = work.workStopTime == 0 ? 0 : vehicle.lastGps.sumWorkStopTime + work.workStopTime;
			
		}
		return work;
	}
	
	public void getRealOilCal(GpsSignalWork work){
		
		LinkedList<Double> tmpList = new LinkedList<Double>();
		if(vehicle.lastList == null){
			vehicle.lastList = new LinkedList<Double>();
		}
		vehicle.lastList.add(signal.oilAmtCal);
		if(vehicle.lastList.size()>5){
			vehicle.lastList.remove(0);
			tmpList.addAll(vehicle.lastList);
			Collections.sort(tmpList);
		}
		
		if(tmpList.size() == 5){			
			//开始算
			//该list自动排序
			if(Math.abs(tmpList.get(tmpList.size()-1)-tmpList.get(0))  <= 1){
				//结果 OK
				Double total = 0.0;
				for(Double d:tmpList){
					total +=d;
				}
				Double result = total/5; 
				work.realOilCal = result;
			}else{
				//结果异常 取上一条的稳定值
				work.realOilCal = vehicle.lastGps.realOilCal;
			}
		}else{
			//条数不满
			work.realOilCal = signal.realOilCal;
		}
		
		signal.realOilCal = work.realOilCal;
	}
	
}
